package com.briup.gsauestore.service.impl;

import com.briup.gsauestore.bean.Category;
import com.briup.gsauestore.bean.vo.CategoryVO;
import com.briup.gsauestore.dao.ICategoryDao;
import com.briup.gsauestore.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author adam
 * @date 2022/1/12
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    ICategoryDao categoryDao;

    @Override
    public List<CategoryVO> findAllCategory() {
//        构建一个存储分类组的集合
        ArrayList<CategoryVO> categoryVoList = new ArrayList<>();
//        查询一级标题
        List<Category> list = categoryDao.findByParentIdIsNull();
        for (Category category : list) {
//            通过一级标题查询二级标题
            List<Category> categories = categoryDao.findByParentId(category.getId());
//            构建一个分类组 =一个分类组包含一个一级分类和一组二级分类
            CategoryVO vo = new CategoryVO(category,categories);
//            将构建出来的对象存储到集合
            categoryVoList.add(vo);
        }
        return categoryVoList;
    }
}
